/*
 * Created on 18-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hotel.ejb;

import hotel.exception.JaReservatException;
import hotel.interfaces.*;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Date;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;

/**
 * @ejb.bean name="Hotel"
 *           display-name="Name for Hotel"
 *           description="Description for Hotel"
 *           jndi-name="ejb/Hotel"
 *           type="Stateless"
 *           view-type="both"
 * 
 * @ejb.util generate="physical"
 */
public class HotelBean implements SessionBean {
	
	String nom = null;
	ReservaFacadeLocalHome reservaFacadeHome = null;
	ClientFacadeLocalHome clientFacadeHome = null;
	HabitacioFacadeLocalHome habitacioFacadeHome = null;
	

	/**
	 * 
	 */
	public HotelBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext ctx)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub
	}

	/**
	 * Default create method
	 * 
	 * @throws CreateException
	 * @ejb.create-method
	 */
	public void ejbCreate() throws CreateException {
		// TODO Auto-generated method stub
		/*
		 * Propietat definida al Descriptor: ejb-jar.xml
		 <session>
		 ...
			 <env-entry>
				 <description>Nom del hotel</description>
				 <env-entry-name>nomHotel</env-entry-name>
				 <env-entry-type>java.lang.String</env-entry-type>
				 <env-entry-value>Marble Station Hotel</env-entry-value>
			 </env-entry>
		 ...
		 </session>
		 */
		
		try {
			Context initCtx = new InitialContext();
			Context myEnv = (Context) initCtx.lookup("java:comp/env");
			this.nom = (String) myEnv.lookup("nomHotel");
		} catch (NamingException e) {
			throw new EJBException("NamingException!");
		}
		
		// Obtenim Home's
		try {
			clientFacadeHome = ClientFacadeUtil.getLocalHome();
			habitacioFacadeHome = HabitacioFacadeUtil.getLocalHome();
			reservaFacadeHome = ReservaFacadeUtil.getLocalHome();
		} catch (Exception e) {
			throw new CreateException(e.getMessage());
		}
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getNom() {
		// TODO Auto-generated method stub
		return nom;
	}
	
	// CLIENTS
	///////////////
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setNomClient(Integer DNI, String Nom) throws CreateException, FinderException {
		// TODO Auto-generated method stub
		ClientFacadeLocal c = clientFacadeHome.create();
		c.setNom(DNI, Nom);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getNomClient(Integer DNI) throws CreateException, FinderException {
		// TODO Auto-generated method stub
		ClientFacadeLocal c = clientFacadeHome.create();
		return c.getNom(DNI);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void altaClient(Integer DNI, String nom) throws CreateException {
		// TODO Auto-generated method stub
		ClientFacadeLocal c = clientFacadeHome.create();
		c.alta(DNI, nom);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void baixaClient(Integer DNI) throws FinderException, RemoveException, CreateException {
		// TODO Auto-generated method stub
		ClientFacadeLocal c = clientFacadeHome.create();
		c.baixa(DNI);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Collection llistatClients() throws FinderException, CreateException {
		// TODO Auto-generated method stub
		ClientFacadeLocal c = clientFacadeHome.create();
		return c.llistat();
	}
	
	// HABITACIONS
	//////////////////
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setTipusHabitacio(Integer ID, Integer tipus) throws FinderException, CreateException {
		// TODO Auto-generated method stub
		HabitacioFacadeLocal h = habitacioFacadeHome.create();
		h.setTipus(ID, tipus);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Integer getTipusHabitacio(Integer ID) throws FinderException, CreateException {
		// TODO Auto-generated method stub
		HabitacioFacadeLocal h = habitacioFacadeHome.create();
		return h.getTipus(ID);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void habilitarHabitacio(Integer ID, Integer tipus) throws CreateException {
		// TODO Auto-generated method stub
		HabitacioFacadeLocal h = habitacioFacadeHome.create();
		h.habilitar(ID, tipus);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void deshabilitarHabitacio(Integer ID) throws FinderException, RemoveException, CreateException {
		// TODO Auto-generated method stub
		HabitacioFacadeLocal h = habitacioFacadeHome.create();
		h.deshabilitar(ID);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Collection llistatHabitacions() throws FinderException, CreateException {
		// TODO Auto-generated method stub
		HabitacioFacadeLocal h = habitacioFacadeHome.create();
		return h.llistat();
	}
	
	// RESERVES
	//////////////
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setDates(Integer ID, Date desde, Date fins) throws FinderException, CreateException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		r.setDates(ID, desde, fins);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getDesdeDataReserva(Integer ID) throws FinderException, CreateException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		return r.getDesdeData(ID);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getFinsDataReserva(Integer ID) throws FinderException, CreateException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		return r.getFinsData(ID);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void reservar(Integer ID, Integer clientDNI, Collection habitacionsID, Date desde, Date fins) throws FinderException, CreateException, JaReservatException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		r.reservar(ID, clientDNI, habitacionsID, desde, fins);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void anularReserva(Integer ID) throws RemoveException, FinderException, CreateException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		r.anular(ID);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Collection llistatReserves() throws FinderException, CreateException {
		// TODO Auto-generated method stub
		ReservaFacadeLocal r = reservaFacadeHome.create();
		return r.llistat();
	}
}